package com.yyf.springboot2.controller;

import com.alibaba.fastjson.JSONObject;
import com.yyf.springboot2.common.Constants;
import com.yyf.springboot2.common.ImageCodeUtil;
import com.yyf.springboot2.common.ResultBean;
import com.yyf.springboot2.common.WebUtil;
import com.yyf.springboot2.entity.TArticle;
import com.yyf.springboot2.entity.TGoods;
import com.yyf.springboot2.mapper.TGoodsMapper;
import com.yyf.springboot2.service.TArticleService;
import com.yyf.springboot2.service.TGoodsService;
import org.apache.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yyf
 * @since 2019-05-29
 */
@Controller
@RequestMapping("/tGoods")
public class TGoodsController extends  BaseController{

    @Resource
    private TGoodsService goodsService;
    @Resource
    private TArticleService articleService;



    @GetMapping("/index/goodsList")
    @ResponseBody
    private ResultBean goodsIndex(@RequestParam(value = "categoryId",required = false) Integer categoryId,
                                  @RequestParam(value = "nameLike",required = false) String serchName,
                                  @RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                                  @RequestParam(value = "pageSize",required = false ,defaultValue = "10") Integer pageSize){
        System.out.println(page +":page" + pageSize +"pageSize");
        String lu = Constants.wildcard;
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("stat", 0);
        if(categoryId == null){
            map.put("goodsType", 1);
        }else{
            map.put("type", categoryId);
        }

        if(serchName != null &&serchName.length() > 0){
            map.put("serarchName", serchName);
        }
        List<TGoods> goodsList = goodsService.getGoodsList(map, pageSize, page);

        System.out.println(map.get("type")+"type");

            for (int i = 0; i < goodsList.size(); i++) {
                System.out.println(goodsList.get(i));
                System.out.println("***");
            }



        if(goodsList .size() <= 0){
            return  ResultBean.build(404,"没有数据",null);
        }else{
//            for (TGoods tGoods : goodsList) {
//                tGoods.getImage();
//                System.out.println(lu +"/target/static/goodsImg");
//                File file=new File(lu +"/target/static/goodsImg", tGoods.getImage());
//                String base64="";
//                String suffix="";
//                if(file.exists()){
//                    base64=ImageCodeUtil.imageToBase64(file.getAbsolutePath());
//                    suffix = tGoods.getImage().substring(tGoods.getImage().lastIndexOf(".") + 1);
//                }
////			map.put("base64", "data:image/"+suffix+";base64,"+base64);
//
//                tGoods.setImage("data:image/"+suffix+";base64,"+base64);
//
//            }
            map.put("goodsList", goodsList);
            JSONObject jsons = new JSONObject(map);
            System.out.println("/index/goodsList");
            return  ResultBean.build(200,"成功",jsons);
        }


    }



    @ResponseBody
    @PostMapping("/upfile")
    private ResultBean goodsSave(@RequestParam(value="imageFile")MultipartFile imageFile){

        String wildcard1 = Constants.wildcard1;
        String lu = Constants.wildcard;
//        if (imageFile.length <= 0) {
//            return ResultBean.build(401,"文件不能为空",null);
//        }
        List<MultipartFile> fileList = new ArrayList<MultipartFile>();
//
//        for (int i = 0; i < imageFile.length; i++) {
//            if(imageFile[i] != null && imageFile[i].getOriginalFilename() != null && imageFile[i].getOriginalFilename().length() > 0 && imageFile[i].getSize() > 0L){
//                fileList.add(imageFile[i]);
//            }
//        }

        fileList.add(imageFile);
//        String fileName = imageFile.getOriginalFilename(); //获取内容中有小数点，需要去掉
//        fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."), file.getOriginalFilename().length());
        for (int i = 0; i < fileList.size(); i++) {
//            if(fileName.length() > 0){
                //随机图片名
            String  fileName = 	UUID.randomUUID().toString().replaceAll("-", "")+new Date().toString()+".jpg";
//            }

            String filePath = lu+"/target/static/goodsImg/";

            File dest = new File(filePath ,fileName);


            try {
                fileList.get(i).transferTo(dest);
                return  ResultBean.build(200,"成功",null);
            } catch (IOException e) {
                e.printStackTrace();
                return  ResultBean.build(501,"上传图片失败",null);
            }

        }


        return  ResultBean.build(501,"上传图片失败",null);
    }


    @GetMapping("/goodsdetails")
    @ResponseBody
    public  ResultBean goodesDetails(@RequestParam(value = "id") Integer id){
        TGoods goods = goodsService.getgoodsDetails(id);
        if(goods != null ){
            return ResultBean.build(200,"访问成功",goods);
        }
        return  ResultBean.build(404,"商品不存在",null);
    }



}
